package com.allwees.bs.module.wms.service.impl;

import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.wms.bo.WmsShippingBo;
import com.allwees.bs.module.wms.dao.WmsPickOrderDao;
import com.allwees.bs.module.wms.entity.WmsAllocateOrderSku;
import com.allwees.bs.module.wms.entity.WmsPickOrder;
import com.allwees.bs.module.wms.entity.WmsPickOrderSku;
import com.allwees.bs.module.wms.req.PickOrderShippingReq;
import com.allwees.bs.module.wms.service.WmsPickOrderService;
import com.allwees.bs.module.wms.service.WmsPickOrderSkuService;
import com.allwees.bs.module.wms.service.WmsShippingOrderService;
import com.allwees.bs.module.wms.util.WmsNoUtil;
import com.allwees.bs.module.wms.wf.WmsPickOrderAction;
import com.allwees.bs.module.wms.wf.WmsPickOrderState;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.common.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * wms 分拣订单
 *
 * @author mouhn
 * @since 2020-01-08 13:56
 */
@Service
public class WmsPickOrderServiceImpl extends BoServiceImpl<WmsPickOrderDao, WmsPickOrder> implements WmsPickOrderService {

    @Autowired
    private WmsPickOrderSkuService pickOrderSkuService;
    @Autowired
    private WmsShippingOrderService shippingOrderService;

    @Override
    @Transactional
    public void create(String operatorUuid, List<WmsAllocateOrderSku> skus) {
        WmsPickOrder pickOrder = _gen(operatorUuid);

        for (WmsAllocateOrderSku allocateOrderSku : skus) {
            pickOrderSkuService.create(pickOrder.getUuid(), allocateOrderSku);
        }
    }

    @Override
    @Transactional
    public void shipping(PickOrderShippingReq req) {
        Date now = new Date();
        List<WmsPickOrderSku> pickOrderSkus = pickOrderSkuService.shipping(req.getPickOrderSkuUuids());

        List<WmsPickOrder> pickOrders = _shipped(pickOrderSkus.stream().map(WmsPickOrderSku::getPickOrderUuid).collect(Collectors.toSet()));
        if (CollectionUtils.isEmpty(pickOrders)) {
            return;
        }
        WmsShippingBo shippingBo = new WmsShippingBo()
                .setShippingAt(now)
                .setShippingUserUuid(req.getBranchUserUuid())
                .setLogisticsCompanyUuid(req.getLogisticsCompanyUuid())
                .setExpressCode(req.getExpressCode())
                .setWarehouseUuid(pickOrders.get(0).getWarehouseUuid())
                .setShippingAmount(req.getShippingAmount())
                .setLogisticsAddressUuid(req.getLogisticsAddressUuid())
                .setPickOrderSkus(pickOrderSkuService.findByUuids(req.getPickOrderSkuUuids()));

        shippingOrderService.create(shippingBo);
    }

    private List<WmsPickOrder> _shipped(Set<String> pickOrderUuids) {

        List<WmsPickOrder> pickOrders = findByUuids(pickOrderUuids);
        pickOrders.forEach(pickOrder -> {
            if (pickOrderSkuService.allShipped(pickOrder.getUuid())) {
                return;
            }

            pickOrder.assertStatus(EStatus.WMS_WAIT_PICK,EStatus.WMS_PICKING);

            pickOrder.signal(WmsPickOrderAction.PLATFORM_SHIPPED);
            updateByUuid(pickOrder);
        });

        return pickOrders;
    }

    private WmsPickOrder _gen(String operatorUuid) {
        String uuid = UuidUtil.generate();

        WmsPickOrder pickOrder = new WmsPickOrder(uuid);
        pickOrder.start(WmsPickOrderState.START, WmsPickOrderAction.PLATFORM_JOIN_TO_PICK);

        pickOrder.setNo(WmsNoUtil.genPickOrderNo(uuid))
                .setPickUserUuid(operatorUuid);

        create(pickOrder);
        return pickOrder;
    }
}
